

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.function.Consumer;

public class Test {
    public static void main(String[] args) {
        Animal animal1 = new Animal("zhangsan",14);
        Animal animal2 = new Animal("lisi",13);

        Animal[] animals = {animal1,animal2};

        // Animal 类没有实现Comparable接口,不支持排序
        Arrays.sort(animals);
        System.out.println(Arrays.toString(animals));

        Person person1 = new Person("zhangsan",14);
        Person person2 = new Person("lisi",13);

        Person[] persons = {person1,person2};
        Arrays.sort(persons, new Comparator<Person>() {
            @Override
            public int compare(Person person, Person t1) {
                return person.name.compareTo(t1.name);
                // 如果当前对象应排在参数对象之前,返回小于零的数字
                // 如果当前对象应排在参数对象之后,返回大于零的数字
                // 如果不分先后,返回零
            }
        });

        System.out.println(Arrays.toString(persons));


/*        ArrayList<String> list = new ArrayList<>();

        list.add("zhagnsan");

        list.forEach(new Consumer() {
            @Override
            public void accept(Object o) {
                System.out.println(o);
            }
        });*/

    }
}
